<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>DOM事件机制 | dmf的个人博客</title><meta name="author" content="dmf"><meta name="copyright" content="dmf"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="描述">
<meta property="og:type" content="article">
<meta property="og:title" content="DOM事件机制">
<meta property="og:url" content="https://gitee.com/dmf-c/blog/2021/10/10/js_file/EventMechanism/index.html">
<meta property="og:site_name" content="dmf的个人博客">
<meta property="og:description" content="描述">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img1.baidu.com/it/u=3691791879,443411760&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=889">
<meta property="article:published_time" content="2021-10-09T16:00:00.000Z">
<meta property="article:modified_time" content="2022-10-12T07:32:06.996Z">
<meta property="article:author" content="dmf">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://img1.baidu.com/it/u=3691791879,443411760&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=889"><link rel="shortcut icon" href="/blog/img/favicon.png"><link rel="canonical" href="https://gitee.com/dmf-c/blog/2021/10/10/js_file/EventMechanism/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/blog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/blog/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'DOM事件机制',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-10-12 15:32:06'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">27</div></a><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">0</div></a><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">6</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw iconfont icon-shouyex"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw iconfont icon-zuixinwenzhang_huaban"></i><span> 找文章</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/blog/categories/"><i class="fa-fw iconfont icon-fenlei1"></i><span> 分类</span></a></li><li><a class="site-page child" href="/blog/tags/"><i class="fa-fw iconfont icon-biaoqian1"></i><span> 标签</span></a></li><li><a class="site-page child" href="/blog/archives/"><i class="fa-fw iconfont icon-shijianzhou"></i><span> 归档</span></a></li></ul></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://img1.baidu.com/it/u=3691791879,443411760&amp;fm=253&amp;fmt=auto&amp;app=138&amp;f=JPEG?w=500&amp;h=889')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/blog/">dmf的个人博客</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw iconfont icon-shouyex"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw iconfont icon-zuixinwenzhang_huaban"></i><span> 找文章</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/blog/categories/"><i class="fa-fw iconfont icon-fenlei1"></i><span> 分类</span></a></li><li><a class="site-page child" href="/blog/tags/"><i class="fa-fw iconfont icon-biaoqian1"></i><span> 标签</span></a></li><li><a class="site-page child" href="/blog/archives/"><i class="fa-fw iconfont icon-shijianzhou"></i><span> 归档</span></a></li></ul></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">DOM事件机制</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="fa-fw post-meta-icon far fa-calendar-alt"></i><span class="post-meta-label">发表于</span><time datetime="2021-10-09T16:00:00.000Z" title="发表于 2021-10-10 00:00:00">2021-10-10</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/blog/categories/Javascript/">Javascript</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="DOM事件机制"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="DOM事件级别"><a href="#DOM事件级别" class="headerlink" title="DOM事件级别"></a>DOM事件级别</h2><p>DOM级别一共可以分为四个级别：DOM0级、DOM1级、DOM2级和DOM3级。而DOM事件分为3个级别：<strong>DOM 0级事件处理，DOM 2级事件处理和DOM 3级事件处理。</strong>由于DOM 1级中没有事件的相关内容，所以没有DOM 1级事件。</p>
<p><strong>1. DOM 0级事件</strong></p>
<p>el.onclick&#x3D;function(){}</p>
<pre><code>// 例1
var btn = document.getElementById(&#39;btn&#39;);
btn.onclick = function()&#123;
    alert(this.innerHTML);
&#125;
</code></pre>
<p><strong>当希望为同一个元素&#x2F;标签绑定多个同类型事件的时候（如给上面的这个btn元素绑定3个点击事件），是不被允许的。</strong>DOM0事件绑定，给元素的事件行为绑定方法，<strong>这些方法都是在当前元素事件行为的冒泡阶段(或者目标阶段)执行的。</strong></p>
<p><strong>2. DOM 2级事件</strong></p>
<p>el.addEventListener(event-name, callback, useCapture)</p>
<ul>
<li><p>event-name: 事件名称，可以是标准的DOM事件</p>
</li>
<li><p>callback: 回调函数，当事件触发时，函数会被注入一个参数为当前的事件对象 event</p>
</li>
<li><p>useCapture: 默认是false，代表事件句柄在冒泡阶段执行</p>
<pre><code>  // 例2
  var btn = document.getElementById(&#39;btn&#39;);
  btn.addEventListener(&quot;click&quot;, test, false);
  function test(e)&#123;
      e = e || window.event;
      alert((e.target || e.srcElement).innerHTML);
      btn.removeEventListener(&quot;click&quot;, test)
  &#125;
  //IE9-:attachEvent()与detachEvent()。
  //IE9+/chrom/FF:addEventListener()和removeEventListener()
</code></pre>
</li>
</ul>
<p>IE9以下的IE浏览器不支持 addEventListener()和removeEventListener()，使用 attachEvent()与detachEvent() 代替，因为IE9以下是不支持事件捕获的，所以也没有第三个参数，第一个事件名称前要加on。</p>
<p><strong>3. DOM 3级事件</strong></p>
<p>在DOM 2级事件的基础上添加了更多的事件类型。</p>
<ul>
<li><p>UI事件，当用户与页面上的元素交互时触发，如：load、scroll</p>
</li>
<li><p>焦点事件，当元素获得或失去焦点时触发，如：blur、focus</p>
</li>
<li><p>鼠标事件，当用户通过鼠标在页面执行操作时触发如：dblclick、mouseup</p>
</li>
<li><p>滚轮事件，当使用鼠标滚轮或类似设备时触发，如：mousewheel</p>
</li>
<li><p>文本事件，当在文档中输入文本时触发，如：textInput</p>
</li>
<li><p>键盘事件，当用户通过键盘在页面上执行操作时触发，如：keydown、 keypress</p>
</li>
<li><p>合成事件，当为IME（输入法编辑器）输入字符时触发，如：*compositionstart</p>
</li>
<li><p>变动事件，当底层DOM结构发生变化时触发，如：DOMsubtreeModified</p>
</li>
<li><p>同时DOM3级事件也允许使用者自定义一些事件。</p>
</li>
</ul>
<h2 id="DOM事件模型和事件流"><a href="#DOM事件模型和事件流" class="headerlink" title="DOM事件模型和事件流"></a>DOM事件模型和事件流</h2><p><strong>DOM事件模型分为捕获和冒泡。</strong>一个事件发生后，会在子元素和父元素之间传播（propagation）。这种传播分成三个阶段。</p>
<p>（1）捕获阶段：事件从window对象自上而下向目标节点传播的阶段；</p>
<p>（2）目标阶段：真正的目标节点正在处理事件的阶段；</p>
<p>（3）冒泡阶段：事件从目标节点自下而上向window对象传播的阶段。</p>
<p><strong>DOM事件捕获的具体流程</strong></p>
<p>捕获是从上到下，事件先从window对象，然后再到document（对象），然后是html标签（通过document.documentElement获取html标签），然后是body标签（通过document.body获取body标签），然后按照普通的html结构一层一层往下传，最后到达目标元素。</p>
<p>而事件冒泡的流程刚好是事件捕获的逆过程。<br>接下来我们看个事件冒泡的例子：</p>
<pre><code>// 例3
&lt;div id=&quot;outer&quot;&gt;
    &lt;div id=&quot;inner&quot;&gt;&lt;/div&gt;
&lt;/div&gt;
......
window.onclick = function() &#123;
    console.log(&#39;window&#39;);
&#125;;
document.onclick = function() &#123;
    console.log(&#39;document&#39;);
&#125;;
document.documentElement.onclick = function() &#123;
    console.log(&#39;html&#39;);
&#125;;
document.body.onclick = function() &#123;
    console.log(&#39;body&#39;);
&#125;
outer.onclick = function(ev) &#123;
    console.log(&#39;outer&#39;);
&#125;;
inner.onclick = function(ev) &#123;
    console.log(&#39;inner&#39;);
&#125;;
</code></pre>
<p>正如我们上面提到的onclick给元素的事件行为绑定方法都是在当前元素事件行为的冒泡阶段(或者目标阶段)执行的。</p>
<h2 id="事件代理-事件委托"><a href="#事件代理-事件委托" class="headerlink" title="事件代理(事件委托)"></a>事件代理(事件委托)</h2><p>由于事件会在冒泡阶段向上传播到父节点，因此可以把子节点的监听函数定义在父节点上，由父节点的监听函数统一处理多个子元素的事件。这种方法叫做事件的代理（delegation）。</p>
<p><strong>1. 优点</strong></p>
<ul>
<li>减少内存消耗，提高性能</li>
</ul>
<p>假设有一个列表，列表之中有大量的列表项，我们需要在点击每个列表项的时候响应一个事件</p>
<pre><code>// 例4
&lt;ul id=&quot;list&quot;&gt;
&lt;li&gt;item 1&lt;/li&gt;
&lt;li&gt;item 2&lt;/li&gt;
&lt;li&gt;item 3&lt;/li&gt;
......
&lt;li&gt;item n&lt;/li&gt;
&lt;/ul&gt;
</code></pre>
<p>如果给每个列表项一一都绑定一个函数，那对于内存消耗是非常大的，效率上需要消耗很多性能。借助事件代理，我们只需要给父容器ul绑定方法即可，这样不管点击的是哪一个后代元素，都会根据冒泡传播的传递机制，把容器的click行为触发，然后把对应的方法执行，根据事件源，我们可以知道点击的是谁，从而完成不同的事。</p>
<ul>
<li>动态绑定事件</li>
</ul>
<p>在很多时候，我们需要通过用户操作动态的增删列表项元素，如果一开始给每个子元素绑定事件，那么在列表发生变化时，就需要重新给新增的元素绑定事件，给即将删去的元素解绑事件，如果用事件代理就会省去很多这样麻烦。</p>
<p><strong>2. 如何实现</strong></p>
<p>接下来我们来实现上例中父层元素 #list 下的 li 元素的事件委托到它的父层元素上：</p>
<pre><code>// 给父层元素绑定事件
document.getElementById(&#39;list&#39;).addEventListener(&#39;click&#39;, function (e) &#123;
// 兼容性处理
var event = e || window.event;
var target = event.target || event.srcElement;
// 判断是否匹配目标元素
if (target.nodeName.toLocaleLowerCase === &#39;li&#39;) &#123;
    console.log(&#39;the content is: &#39;, target.innerHTML);
&#125;
&#125;);
</code></pre>
<h2 id="Event对象常见的应用"><a href="#Event对象常见的应用" class="headerlink" title="Event对象常见的应用"></a>Event对象常见的应用</h2><ul>
<li>event. preventDefault()</li>
</ul>
<p><strong>如果调用这个方法，默认事件行为将不再触发。</strong>什么是默认事件呢？例如表单一点击提交按钮(submit)跳转页面、a标签默认页面跳转或是锚点定位等。</p>
<p>很多时候我们使用a标签仅仅是想当做一个普通的按钮，点击实现一个功能，不想页面跳转，也不想锚点定位。</p>
<pre><code>//方法一：
&lt;a href=&quot;javascript:;&quot;&gt;链接&lt;/a&gt;
</code></pre>
<p>也可以通过JS方法来阻止，给其click事件绑定方法，当我们点击A标签的时候，先触发click事件，其次才会执行自己的默认行为</p>
<pre><code>//方法二:
&lt;a id=&quot;test&quot; href=&quot;http://www.cnblogs.com&quot;&gt;链接&lt;/a&gt;
&lt;script&gt;
test.onclick = function(e)&#123;
    e = e || window.event;
    return false;
&#125;
&lt;/script&gt;

//方法三：
&lt;a id=&quot;test&quot; href=&quot;http://www.cnblogs.com&quot;&gt;链接&lt;/a&gt;
&lt;script&gt;
test.onclick = function(e)&#123;
    e = e || window.event;
    e.preventDefault();
&#125;
&lt;/script&gt;
</code></pre>
<p>接下来我们看个例子：输入框最多只能输入六个字符，如何实现？</p>
<pre><code>// 例5
&lt;input type=&quot;text&quot; id=&#39;tempInp&#39;&gt;
&lt;script&gt;
    tempInp.onkeydown = function(ev) &#123;
        ev = ev || window.event;
        let val = this.value.trim() //trim去除字符串首位空格（不兼容）
        // this.value=this.value.replace(/^ +| +$/g,&#39;&#39;) 兼容写法
        let len = val.length
        if (len &gt;= 6) &#123;
            this.value = val.substr(0, 6);
            //阻止默认行为去除特殊按键（DELETE\BACK-SPACE\方向键...）
            let code = ev.which || ev.keyCode;
            if (!/^(46|8|37|38|39|40)$/.test(code)) &#123;
                ev.preventDefault()
            &#125;
        &#125;
    &#125;
&lt;/script&gt;
</code></pre>
<ul>
<li>event.stopPropagation() &amp; event.stopImmediatePropagation()</li>
</ul>
<p>event.stopPropagation() 方法<strong>阻止事件冒泡到父元素，阻止任何父事件处理程序被执行。</strong>上面提到事件冒泡阶段是指事件从目标节点自下而上向window对象传播的阶段。<br>我们在例3的inner元素click事件上，添加event.stopPropagation()这句话后，就阻止了父事件的执行，最后只打印了’inner’。</p>
<pre><code>inner.onclick = function(ev) &#123;
    console.log(&#39;inner&#39;);
    ev.stopPropagation();
&#125;;
</code></pre>
<p><strong>stopImmediatePropagation 既能阻止事件向父元素冒泡，也能阻止元素同事件类型的其它监听器被触发。而 stopPropagation 只能实现前者的效果。</strong>我们来看个例子：</p>
<pre><code>&lt;body&gt;
&lt;button id=&quot;btn&quot;&gt;click me to stop propagation&lt;/button&gt;
&lt;/body&gt;
......
const btn = document.querySelector(&#39;#btn&#39;);
btn.addEventListener(&#39;click&#39;, event =&gt; &#123;
console.log(&#39;btn click 1&#39;);
event.stopImmediatePropagation();
&#125;);
btn.addEventListener(&#39;click&#39;, event =&gt; &#123;
console.log(&#39;btn click 2&#39;);
&#125;);
document.body.addEventListener(&#39;click&#39;, () =&gt; &#123;
console.log(&#39;body click&#39;);
&#125;);
// btn click 1
</code></pre>
<p>如上所示，使用 stopImmediatePropagation后，点击按钮时，不仅body绑定事件不会触发，与此同时按钮的另一个点击事件也不触发。</p>
<ul>
<li>event.target &amp; event.currentTarget</li>
</ul>
<p>老实说这两者的区别，并不好用文字描述，我们先来看个例子：</p>
<pre><code>&lt;div id=&quot;a&quot;&gt;
&lt;div id=&quot;b&quot;&gt;
    &lt;div id=&quot;c&quot;&gt;&lt;div id=&quot;d&quot;&gt;&lt;/div&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;script&gt;
document.getElementById(&#39;a&#39;).addEventListener(&#39;click&#39;, function(e) &#123;
    console.log(
    &#39;target:&#39; + e.target.id + &#39;&amp;currentTarget:&#39; + e.currentTarget.id
    )
&#125;)
document.getElementById(&#39;b&#39;).addEventListener(&#39;click&#39;, function(e) &#123;
    console.log(
    &#39;target:&#39; + e.target.id + &#39;&amp;currentTarget:&#39; + e.currentTarget.id
    )
&#125;)
document.getElementById(&#39;c&#39;).addEventListener(&#39;click&#39;, function(e) &#123;
    console.log(
    &#39;target:&#39; + e.target.id + &#39;&amp;currentTarget:&#39; + e.currentTarget.id
    )
&#125;)
document.getElementById(&#39;d&#39;).addEventListener(&#39;click&#39;, function(e) &#123;
    console.log(
    &#39;target:&#39; + e.target.id + &#39;&amp;currentTarget:&#39; + e.currentTarget.id
    )
&#125;)
&lt;/script&gt;
</code></pre>
<p>当我们点击最里层的元素d的时候，会依次输出:</p>
<pre><code>target:d&amp;currentTarget:d
target:d&amp;currentTarget:c
target:d&amp;currentTarget:b
target:d&amp;currentTarget:a
</code></pre>
<p>从输出中我们可以看到，event.target指向引起触发事件的元素，而event.currentTarget则是事件绑定的元素，只有被点击的那个目标元素的event.target才会等于event.currentTarget。<strong>也就是说，event.currentTarget始终是监听事件者，而event.target是事件的真正发出者。</strong></p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://gitee.com/dmf-c/blog">dmf</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://gitee.com/dmf-c/blog/2021/10/10/js_file/EventMechanism/">https://gitee.com/dmf-c/blog/2021/10/10/js_file/EventMechanism/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://gitee.com/dmf-c/blog" target="_blank">dmf的个人博客</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"></div><div class="post_share"><div class="social-share" data-image="https://img1.baidu.com/it/u=3691791879,443411760&amp;fm=253&amp;fmt=auto&amp;app=138&amp;f=JPEG?w=500&amp;h=889" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/blog/2021/10/24/js_file/Inherit/"><img class="prev-cover" src="https://img1.baidu.com/it/u=3691791879,443411760&amp;fm=253&amp;fmt=auto&amp;app=138&amp;f=JPEG?w=500&amp;h=889" onerror="onerror=null;src='/blog/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">JavaScript常见的六种继承方式</div></div></a></div><div class="next-post pull-right"><a href="/blog/2021/09/12/js_file/PrototypeChain/"><img class="next-cover" src="https://img1.baidu.com/it/u=3691791879,443411760&amp;fm=253&amp;fmt=auto&amp;app=138&amp;f=JPEG?w=500&amp;h=889" onerror="onerror=null;src='/blog/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">原型与原型链</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">dmf</div><div class="author-info__description"></div></div><div class="card-info-data site-data is-center"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">27</div></a><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">0</div></a><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">6</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">业精于勤，荒于嬉。</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#DOM%E4%BA%8B%E4%BB%B6%E7%BA%A7%E5%88%AB"><span class="toc-number">1.</span> <span class="toc-text">DOM事件级别</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#DOM%E4%BA%8B%E4%BB%B6%E6%A8%A1%E5%9E%8B%E5%92%8C%E4%BA%8B%E4%BB%B6%E6%B5%81"><span class="toc-number">2.</span> <span class="toc-text">DOM事件模型和事件流</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%8B%E4%BB%B6%E4%BB%A3%E7%90%86-%E4%BA%8B%E4%BB%B6%E5%A7%94%E6%89%98"><span class="toc-number">3.</span> <span class="toc-text">事件代理(事件委托)</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Event%E5%AF%B9%E8%B1%A1%E5%B8%B8%E8%A7%81%E7%9A%84%E5%BA%94%E7%94%A8"><span class="toc-number">4.</span> <span class="toc-text">Event对象常见的应用</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/blog/2023/01/08/vue_file/vuenote/" title="vue3入门笔记"><img src="https://img1.baidu.com/it/u=1828439677,2166285235&amp;fm=253&amp;fmt=auto&amp;app=138&amp;f=JPEG?w=400&amp;h=300" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="vue3入门笔记"/></a><div class="content"><a class="title" href="/blog/2023/01/08/vue_file/vuenote/" title="vue3入门笔记">vue3入门笔记</a><time datetime="2023-01-07T16:00:00.000Z" title="发表于 2023-01-08 00:00:00">2023-01-08</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2022/02/20/vue_file/comm/" title="vue组件间通信六种方式"><img src="https://img1.baidu.com/it/u=1828439677,2166285235&amp;fm=253&amp;fmt=auto&amp;app=138&amp;f=JPEG?w=400&amp;h=300" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="vue组件间通信六种方式"/></a><div class="content"><a class="title" href="/blog/2022/02/20/vue_file/comm/" title="vue组件间通信六种方式">vue组件间通信六种方式</a><time datetime="2022-02-19T16:00:00.000Z" title="发表于 2022-02-20 00:00:00">2022-02-20</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2022/02/05/wxEcharts/" title="微信小程序使用 echarts"><img src="https://img1.baidu.com/it/u=1828439677,2166285235&amp;fm=253&amp;fmt=auto&amp;app=138&amp;f=JPEG?w=400&amp;h=300" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="微信小程序使用 echarts"/></a><div class="content"><a class="title" href="/blog/2022/02/05/wxEcharts/" title="微信小程序使用 echarts">微信小程序使用 echarts</a><time datetime="2022-02-04T16:00:00.000Z" title="发表于 2022-02-05 00:00:00">2022-02-05</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2022/01/30/vue_file/vue_router/" title="vue-router学习"><img src="https://img1.baidu.com/it/u=1828439677,2166285235&amp;fm=253&amp;fmt=auto&amp;app=138&amp;f=JPEG?w=400&amp;h=300" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="vue-router学习"/></a><div class="content"><a class="title" href="/blog/2022/01/30/vue_file/vue_router/" title="vue-router学习">vue-router学习</a><time datetime="2022-01-29T16:00:00.000Z" title="发表于 2022-01-30 00:00:00">2022-01-30</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2022/01/16/vue_file/vuex/" title="Vuex使用方法"><img src="https://img1.baidu.com/it/u=1828439677,2166285235&amp;fm=253&amp;fmt=auto&amp;app=138&amp;f=JPEG?w=400&amp;h=300" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="Vuex使用方法"/></a><div class="content"><a class="title" href="/blog/2022/01/16/vue_file/vuex/" title="Vuex使用方法">Vuex使用方法</a><time datetime="2022-01-15T16:00:00.000Z" title="发表于 2022-01-16 00:00:00">2022-01-16</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2023 By dmf</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/blog/js/utils.js"></script><script src="/blog/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>